//
//  ETExampleDemoVC.m
//  Common
//
//  Created by shiqing guo on 2018/10/17.
//  Copyright © 2018年 www.bitkinetic.com. All rights reserved.
//

#import "ETExampleDemoVC.h"
#import "ExcelUnitCell.h"
#import "ExcelFirstColumnView.h"
#import "ExcelTableDefine.h"
#import "Macro.h"
#import "UIView+DDAddition.h"

@interface ETExampleDemoVC () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) ExcelFirstColumnView *leftView;
@property (nonatomic, strong) UIScrollView *rigthScrollView;
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) NSMutableArray *topArr;
@property (nonatomic, strong) NSMutableArray *leftArr;
@property (nonatomic, strong) NSMutableArray *sourceArray;

@property (nonatomic, strong) UITableView *rigthTableView;

@property (nonatomic, assign) NSUInteger iCounts;
@property (nonatomic, assign) NSUInteger iColumns;

@property (nonatomic, assign) CGPoint offset;

// 重新计算excel标题行的宽度
@property (nonatomic, assign) CGFloat ehWidth;
@end

@implementation ETExampleDemoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化数据条数
    self.iCounts = 10;
    self.iColumns = 2;
    
    // 列数组
    self.topArr = [NSMutableArray array];
    self.sourceArray = [NSMutableArray array];
    self.leftArr = [NSMutableArray array];
    
    [self addSubviews];
    [self setData];
}

#pragma mark - 数据处理
- (void)setData
{
    self.offset = self.rigthTableView.contentOffset;
    [self.topArr removeAllObjects];
    [self.sourceArray removeAllObjects];
    
    for (int i = 0; i < self.iCounts; i++) {

        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:self.iColumns];
        for (int j = 0; j < self.iColumns; j++) {
            [arr addObject:[NSString stringWithFormat:@"第%d行,第%d列",i,j]];
        }
        [self.sourceArray addObject:arr];
    }
    for (int j = 0; j < self.iColumns; j++) {
        [self.topArr addObject:[NSString stringWithFormat:@"第%d列",j]];
    }
    
    // 初始化宽度
    self.ehWidth = (UI_SCREEN_WIDTH - leftWidth)/self.iColumns > mWidth?(UI_SCREEN_WIDTH - leftWidth)/self.iColumns:mWidth;
    
    
    // 有数据后，才能给列头填充数据
    for (int i = 0; i < self.headView.subviews.count; i++) {
        UIView *view = [self.headView.subviews objectAtIndex:i];
        [view removeFromSuperview];
    }
    for(int i = 0; i < self.iColumns; i++){
        UILabel *lblHead=[[UILabel alloc]initWithFrame:CGRectMake(i * (self.ehWidth + 0.5), 0, self.ehWidth - 0.5, mHeight - 1)];
        lblHead.backgroundColor = [UIColor whiteColor];
        lblHead.text = self.topArr[i];
        lblHead.textAlignment = NSTextAlignmentCenter;
        [self.headView addSubview:lblHead];
    }
    
    // 左边
    [self.leftArr removeAllObjects];
    for (int i = 0; i < self.iCounts; i++) {
        [self.leftArr addObject:[NSString stringWithFormat:@"%d", i]];
    }
    
    self.leftView.dataList = self.leftArr;
    CGFloat lHeight = ((cellHeight * self.iCounts + mHeight) > EPContentViewHeight-bottomSpace)?EPContentViewHeight-bottomSpace:(cellHeight * self.iCounts + mHeight);
    self.leftView.height = lHeight;
    self.headView.width = self.ehWidth * self.iColumns;
    self.rigthScrollView.contentSize = CGSizeMake(self.ehWidth*self.iColumns, lHeight);
    self.rigthScrollView.width = UI_SCREEN_WIDTH - leftWidth;
    self.rigthScrollView.height = lHeight;
    self.rigthTableView.contentSize = CGSizeMake(self.ehWidth*self.iColumns, lHeight);
    self.rigthTableView.width = self.ehWidth * self.iColumns;
    self.rigthTableView.height = lHeight;
    
    [self.rigthTableView reloadData];
    self.rigthTableView.contentOffset = self.offset;
    CGPoint leftOffset = self.leftView.tableView.contentOffset;
    leftOffset.y = self.offset.y;
    self.leftView.tableView.contentOffset = leftOffset;
}

#pragma mark - 懒加载
- (UITableView *)rigthTableView
{
    if (!_rigthTableView) {
        _rigthTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.ehWidth * self.iColumns, ScreenHeight - 64) style:UITableViewStylePlain];
        _rigthTableView.delegate = self;
        _rigthTableView.dataSource = self;
        _rigthTableView.allowsSelection = NO;
        _rigthTableView.bounces = NO;
        _rigthTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _rigthTableView.showsVerticalScrollIndicator = NO;
        _rigthTableView.showsHorizontalScrollIndicator = NO;
        [_rigthTableView registerClass:[ExcelUnitCell class] forCellReuseIdentifier:@"ExcelUnitCell"];
        if (@available(iOS 11.0, *)) {
            _rigthTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _rigthTableView;
}


- (void)addSubviews {

    self.view.backgroundColor = [UIColor whiteColor];
    
    // 头部
    UIView *Headview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.ehWidth * self.iColumns , mHeight)];
    self.headView = Headview;
    self.headView.backgroundColor = [UIColor blackColor];
    
    // 左边
    CGFloat lHeight = ((cellHeight * self.iCounts + mHeight) > EPContentViewHeight-bottomSpace)?EPContentViewHeight-bottomSpace:(cellHeight * self.iCounts + mHeight);
    self.leftView = [[ExcelFirstColumnView alloc] initWithFrame:CGRectMake(0, 0, leftWidth, lHeight)];
    self.leftView.dataList = self.leftArr;
    self.leftView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.leftView];
    
    // 右边
    self.rigthScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(leftWidth, 0, ScreenWidth - leftWidth, ScreenHeight)];
    [self.rigthScrollView addSubview:self.rigthTableView];
    self.rigthScrollView.showsVerticalScrollIndicator = NO;
    self.rigthScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.rigthScrollView];
    
    // 底部按钮, 随机参数使用
    UIButton *btnReload = [[UIButton alloc] initWithFrame:CGRectMake(0, EPContentViewHeight-44.0f, UI_SCREEN_WIDTH, 44.0f)];
    [btnReload setTitle:@"重新加载" forState:UIControlStateNormal];
    [btnReload addTarget:self action:@selector(btnReloadAction:) forControlEvents:UIControlEventTouchUpInside];
    [btnReload setBackgroundColor:RandomColor];
    [self.view addSubview:btnReload];
}

#pragma mark - Buttion Aciton
- (void)btnReloadAction:(id)sender
{
    self.iCounts += arc4random_uniform(10);
//    self.iColumns += arc4random_uniform(10);
    [self setData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sourceArray.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return cellHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return mHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ExcelUnitCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ExcelUnitCell" forIndexPath:indexPath];
    cell.ehWidth = self.ehWidth;
    cell.dataSource = self.sourceArray[indexPath.row];
    return cell;
}

//同步左侧右侧
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offsetY = scrollView.contentOffset.y;
    CGPoint timeOffset = self.leftView.tableView.contentOffset;
    timeOffset.y = offsetY;
    self.leftView.tableView.contentOffset = timeOffset;
    if(0 == offsetY) {
        self.leftView.tableView.contentOffset = CGPointZero;
    }
    NSLog(@"右边偏移%f", offsetY);
}

@end
